package pl.edu.romanow.model

import com.tinkerpop.blueprints.pgm.{Edge, Vertex}
import scala.collection.JavaConversions._
import net.liftweb.common.{Full, Box, Empty}
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph

/**
 * Created by IntelliJ IDEA.
 * User: jar
 * Date: 18.04.12
 * Time: 09:54
 * To change this template use File | Settings | File Templates.
 */

object GraphHelper {
  implicit def Vertex2OSVertex(v: Vertex) = SVertex(v)

  implicit def SVertex2Vertex(sv: SVertex) = sv.v


  def subGraph(edges: Iterable[Edge]): TinkerGraph = {

    val g = new TinkerGraph()

    def getOrCrateV(id: AnyRef) = g.getVertex(id) match {
      case null => g.addVertex(id)
      case sth => sth
    }

    for (e <- edges if g.getEdge(e.getId) == null) {
      val in = getOrCrateV(e.getInVertex.getId)
      val out = getOrCrateV(e.getOutVertex.getId)
      g.addEdge(e.getId, in, out, e.getLabel)
    }

    g
  }

}

case class SVertex(v: Vertex) {

  def \(name: String, value: String): Vertex = {

    v.setProperty(name, value)
    v
  }

  def \(name: String): String =
    v.getProperty(name) match {
      case null => ""
      case sth => sth.toString
    }


  def \?(name: String): Box[String] =
    v.getProperty(name) match {
      case null => Empty
      case sth => Full(sth.toString)
    }


  def ^ = v.getId + ": " + this \ "FacebookId"

  def ^^ = "document:" + v.getPropertyKeys.map(n => n + ":" + this \ n).mkString(", ")

}

case class SEdge(e: Edge) {

}

